<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\home\controller;
use cmf\controller\UserBaseController;
use think\Db;
class MemberController extends UserBaseController
{
    public function index(){

    return $this->fetch();
    }
    /*
     * 添加会员
     * 先查询该店铺的店长
     * 查询该店长得店铺
     * 然后在结合卡号 手机号去查询是否存在该会员
     * 先判断会员是否存在
     * 在判断同店铺下是否有该手机号
     */
    function memadd(){
        $uinfo=cmf_get_current_user();
        $shop=manage($uinfo['store_id']);
        if($_POST)
        {
            $where['store_id']=array('in',$shop);
            $where['card_id']=$_POST['card_id'];
            $card=DB::name('member')->where($where)->find();
            if($card)
            {
                $con['status']=0;
                $con['con']="会员卡号已经存在！";
                $this->success($con);
            }
            $cause['store_id']=array('in',$shop);
            $cause['mobile']=$_POST['mobile'];
            $check=DB::name('Member')->where($cause)->find();
            if($check)
            {
                $con['status']=0;
                $con['con']="该手机号已是会员！";
                $this->success($con);
            }
            $times = time();
            $time = substr($times,3);
            $rand = rand(10,99);
            $id=$rand.$time;//2018/05/30  name 乾坤
            $date['id']=$id;
            $date['card_id']=$_POST['card_id'];
            $date['uname']=$_POST['uname'];
            $date['sex']=$_POST['sex'];
            $date['birthday']=strtotime($_POST['date']);
            $date['mobile']=$_POST['mobile'];
            $date['ctime']=time();
            $date['mop']=0;
            $date['store_id']=$uinfo['store_id'];
            $date['mempass']=cmf_password($_POST['pasword']);
            $re=Db::name('Member')->insert($date);
            if($re)
            {
                $why['id']=$id;
                $member=Db::name('Member')->where($why)->find();
                $da=array(
                    'id'    =>time().rand(1000,9999),//2018/05/30   name 乾坤
                    'userid'=>$id,
                    'cardid'=>$member['card_id'],
                    'storeid'=>$member['store_id']
                );
                $fin=DB::name('finance')->insert($da);
                $das=array(
                    'id'    =>time().rand(1000,9999),//2018/05/30   name 乾坤
                    'userid'=>$id,
                    'cardid'=>$member['card_id'],
                    'storeid'=>$member['store_id']
                );
                $int=DB::name('integral')->insert($das);
                $con['status']=1;
                $con['con']="'恭喜您！注册成功'";
                $this->success($con);
            }
        }else{
            return $this->fetch();
        }
    }
    function findmem(){

        return $this->fetch();
    }
    public function center(){

       
      return $this->fetch();
    }
    
    /*
     * 输入会员手机号
     * 输入手机和商店id
     * 会员卡查询和手机号查询
     * */
    function recharge(){
        $mobile=$_POST['phone'];
        $type=$_POST['type'];
        $uinfo=cmf_get_current_user();
        $shop=manage($uinfo['store_id']);
        if($_POST){
            if(!empty($mobile)){

                $where['store_id']=array('in',$shop);
                if($type == 'cord')
                {
                    $where['card_id']=$mobile;
                }else{
                    $where['mobile']=$mobile;
                }
                $member=DB::name('member')->where($where)->find();
                if($member ==null){
                    $state['state']=0;
                    $state['con']='不好意思,没有该会员';
                    return $this->success($state);
                }else{
                    $balance=DB::name('finance')->where(array('userid'=>$member['id'],'storeid'=>$uinfo['store_id']))->find();
                    $integral=DB::name('integral')->where(array('userid'=>$member['id'],'storeid'=>$uinfo['store_id']))->find();
                    if($balance['money'])
                    {
                        $member['balance']=$balance['money'];
                    }else{
                        $member['balance']=0;
                    }
                    if($integral['num'])
                    {
                        $member['integral']=$integral['num'];
                    }else{
                        $member['integral']=0;
                    }
                    if(empty($_SESSION['think']['phoneone']))
                    {
                        $array['mop']=$member['mobile'];
                        session('phoneone', $array);
                    }
                    $state['state']=1;
                    $state['con']=$member;
                    return $this->success($state);
                }
            }else{
                $state['state']=0;
                $state['con']='不好意思,没有该会员';
                return $this->success($state);
            }
        }
        return $this->fetch('member/recharge');
    }    
   /*
    * 2018-05-23 王磊  完善没有设置积分时候
    * 2018-05-29 同一个店长下的会员可以互通
    *            查询折扣状态
    * */
    public function chongzhi(){
    
        return $this->fetch();
    }
   function chongPost(){
        $uinfo=cmf_get_current_user();//管理员信息
        $shop=manage($uinfo['store_id']);
        $config=DB::name('config')->where(array('state'=>1,'storeid'=>$uinfo['store_id']))->order('time desc')->find();//充值配置
        $data['phone']=$_POST['phone'];
        $data['money']=$_POST['money'];
        $type=$_POST['type'];
        $where['store_id']=array('in',$shop);
        if($type=='cord')
        {
            $where['card_id']= $data['phone'];
        }elseif($type=='phone'){
            $where['mobile']= $data['phone'];
        }
        $member=DB::name('member')->where($where)->find();//会员信息
        if($config)
        {
            if($data['money']<$config['min']){//最小金额
                $state['sta']=0;
                $state['con']="充值金额不得少于".$config['min'];
                $this->success($state);
            }
            if($data['money']>$config['max']){//最小金额
                $state['sta']=0;
                $state['con']="充值金额不得大于".$config['max'];
                $this->success($state);
            }
            $zmoney=$data['money']*($config['top_money']/100);
            $money=$data['money']*($config['top_money']/100)+$data['money']; //赠的送钱
        }else{
            $money=$data['money'];
        }
        /*
         * 充值流水
         * 查询是否有该会员
         * */
        $cause['userid']=$member['id'];
        $cause['storeid']=array('in',$shop);
        $cKM=DB::name('finance')->where($cause)->find();
        if($cKM){
            $TMoney=$cKM['money']+$money;
            $re=DB::name('finance')->where($cause)->setField('money',$TMoney);
            $dat['money']=$TMoney;
        }else{
            //新增
            $da_M=array(
                'id'    =>time().rand(1000,9999),//2018/05/30 name 乾坤
                'userid'=>$member['id'],
                'cardid'=>$member['card_id'],
                'storeid'=>$member['store_id'],
                'money'=>$money
            );
            $re=Db::name('finance')->insert($da_M);
            $dat['money']=$money;
        }
        //记录充值流水
       if($re)
       {
           $da_M_rec_c=array(
               'id'    =>time().rand(1000,9999), //2018/05/30 name 乾坤
               'userid'=>$member['id'],
               'storeid'=>$member['store_id'],
               'money'=>$data['money'],
               'operatorid'=>$uinfo['id'],
               'remark'=>'充值',
               'ctime'=>time()
           );
           $re=Db::name('finance_record')->insert($da_M_rec_c);

           if($config['top_money']>0){
               //记录赠送余额流水
               $da_M_rec=array(
                   'id'    =>time().rand(1000,9999),//2018/05/30 name 乾坤
                   'userid'=>$member['id'],
                   'storeid'=>$member['store_id'],
                   'money'=>$zmoney,
                   'operatorid'=>$uinfo['id'],
                   'remark'=>'充值赠送',
                   'ctime'=>time()
               );
               $state=Db::name('finance_record')->insert($da_M_rec);
           }
       }
        //赠送积分
       if($config['top_intintegral']!=0)
       {
           $integral=$data['money']*($config['top_intintegral']/100);//赠送积分数量
           if($integral){
               //余额增加
               $cKin=DB::name('integral')->where($cause)->find();
               if($cKin){
                   $Tintegral=$cKin['num']+$integral;
                   $re=DB::name('integral')->where($cause)->setField('num',$Tintegral);
                   $dat['integral']=$Tintegral;
               }else{
                   //新增
                   $da_in=array(
                       'id'    =>time().rand(1000,9999),//2018/05/30  name 乾坤
                       'userid'=>$member['id'],
                       'cardid'=>$member['card_id'],
                       'storeid'=>$member['store_id'],
                       'num'=>$integral
                   );
                   Db::name('integral')->insert($da_in);
                   $dat['integral']=$integral;
               }
               //记录流水
               $da_in_rec=array(
                   'id'    =>time().rand(1000,9999),//2018/05/30  name 乾坤
                   'userid'=>$member['id'],
                   'storeid'=>$member['store_id'],
                   'num'=>$integral,
                   'operatorid'=>$uinfo['id'],
                   'remark'=>'充值赠送',
                   'ctime'=>time()
               );
               Db::name('integral_record')->insert($da_in_rec);
           }
       }
       $dat['sta']=1;
       $dat['con']="充值成功";
       $this->success($dat);
   }
   function memberList(){
       $uinfo=cmf_get_current_user();
       $request = input('request.');
       $where='1=1';
       $where .=' and store_id='.$uinfo['store_id'];
       if(!empty($request['cardid'])){
           $where .= ' and card_id = "'.$request['cardid'].'"';
       }
       if(!empty($request['start_time'])){
           $where .= ' and DATE( FROM_UNIXTIME( ctime ) ) >= "'.$request['start_time'].'"';
       }else{
           $where .= ' and DATE( FROM_UNIXTIME( ctime ) ) >= "'.date('Y-m-d',time()).'"';
       }
       if(!empty($request['end_time'])){
           $where .= ' and DATE( FROM_UNIXTIME( ctime ) ) <= "'.$request['end_time'].'"';
       }
       $list=DB::name('member')->where($where)->order('id desc')->paginate(8);
       $list->appends($request);
       $this->assign("page", $list);
       $this->assign("list", $list);
       $this->assign("uinfo", $uinfo);
       return $this->fetch();
   }
}